﻿#region

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InfoTech.Utilities.Image;
using InfoTech.VinhSon.Bussiness;
using InfoTech.VinhSon.Models;
using Telerik.Web.Mvc;

#endregion

namespace VinhSon.Areas.Administrator.Controllers
{
    public class ArticleController : BaseAdministratorController
    {
        #region Article

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(UnitOfWork.ArticleRepository.Select(c => c.Inactive == false || c.Inactive == null)
                                                .Select(c => new
                                                                 {
                                                                     c.Id,
                                                                     c.Name,
                                                                     c.Activated,
                                                                     Category = c.Category.Name,
                                                                     c.Url,
                                                                     c.LastModified
                                                                 })));
        }

        public ActionResult Add()
        {
            ViewBag.Categories = UnitOfWork.CategoryRepository.Select(c => c.Inactive == false || c.Inactive == null,
                                                                      c => c.Name)
                                           .Select(c => new
                                                            {
                                                                c.Id,
                                                                c.Name
                                                            })
                                           .ToList()
                                           .Select(c => new SelectListItem
                                                            {
                                                                Text = c.Name,
                                                                Value = c.Id + ""
                                                            });
            return View(new ArticleModel
                            {
                                Activated = true
                            });
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(ArticleModel articleModel)
        {
            if (ModelState.IsValid)
            {
                Article article = articleModel;
                if (UnitOfWork.ArticleRepository.CheckInsertArticle(article))
                {
                    if (articleModel.CategoryId.HasValue)
                        article.Category = UnitOfWork.CategoryRepository.SelectbyId(articleModel.CategoryId.Value);
                    article.Url = ConvertToUnSign(article.Name);
                    UnitOfWork.ArticleRepository.Insert(article);
                    UnitOfWork.Save();

                    // save document
                    HttpPostedFileBase file = Request.Files[0];
                    if (file != null)
                        if (file.ContentLength > 0)
                        {
                            article.Thumbnail = string.Format("{0}_{1}",
                                                              article.Id,
                                                              file.FileName);
                            ProcessScaleImage.CenterInside(file.InputStream,
                                                           0,
                                                           0)
                                             .Save(string.Format("{0}{1}",
                                                                 Server.MapPath("~/Resource/Article/"),
                                                                 article.Thumbnail));
                            UnitOfWork.Save();
                        }

                    return RedirectToAction("Index");
                }
            }
            ViewBag.Categories = UnitOfWork.CategoryRepository.Select(c => c.Inactive == false || c.Inactive == null,
                                                                      c => c.Name)
                                           .Select(c => new
                                                            {
                                                                c.Id,
                                                                c.Name
                                                            })
                                           .ToList()
                                           .Select(c => new SelectListItem
                                                            {
                                                                Text = c.Name,
                                                                Value = c.Id + ""
                                                            });
            return View(articleModel);
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                Article article = UnitOfWork.ArticleRepository.Select(c => c.Id == guid,
                                                                      "Category")
                                            .FirstOrDefault();
                if (article != null)
                {
                    ViewBag.Categories = UnitOfWork.CategoryRepository.Select(c => c.Inactive == false || c.Inactive == null,
                                                                              c => c.Name)
                                                   .Select(c => new
                                                                    {
                                                                        c.Id,
                                                                        c.Name
                                                                    })
                                                   .ToList()
                                                   .Select(c => new SelectListItem
                                                                    {
                                                                        Text = c.Name,
                                                                        Value = c.Id + ""
                                                                    });

                    ArticleModel articleModel = article;
                    return View(articleModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(ArticleModel articleModel)
        {
            if (ModelState.IsValid)
            {
                Article article = UnitOfWork.ArticleRepository.SelectbyId(articleModel.Id);
                articleModel.Update(article);
                if (UnitOfWork.ArticleRepository.CheckUpdateArticle(article))
                {
                    if (articleModel.CategoryId.HasValue)
                        article.Category = UnitOfWork.CategoryRepository.SelectbyId(articleModel.CategoryId.Value);
                    article.Url = ConvertToUnSign(article.Name);

                    // save document
                    HttpPostedFileBase file = Request.Files[0];
                    if (file != null)
                        if (file.ContentLength > 0)
                        {
                            // delete old resource
                            if (!string.IsNullOrEmpty(article.Thumbnail)
                               && System.IO.File.Exists(string.Format("{0}{1}",
                                                                      Server.MapPath("~/Resource/Article/"),
                                                                      article.Thumbnail)))
                                System.IO.File.Delete(string.Format("{0}{1}",
                                                                    Server.MapPath("~/Resource/Article/"),
                                                                    article.Thumbnail));

                            article.Thumbnail = string.Format("{0}_{1}",
                                                              article.Id,
                                                              file.FileName);
                            ProcessScaleImage.CenterInside(file.InputStream,
                                                           0,
                                                           0)
                                             .Save(string.Format("{0}{1}",
                                                                 Server.MapPath("~/Resource/Article/"),
                                                                 article.Thumbnail));
                        }

                    UnitOfWork.ArticleRepository.Update(article);
                    UnitOfWork.Save();
                    return RedirectToAction("Index");
                }
            }
            ViewBag.Categories = UnitOfWork.CategoryRepository.Select(c => c.Inactive == false || c.Inactive == null,
                                                                      c => c.Name)
                                           .Select(c => new
                                                            {
                                                                c.Id,
                                                                c.Name
                                                            })
                                           .ToList()
                                           .Select(c => new SelectListItem
                                                            {
                                                                Text = c.Name,
                                                                Value = c.Id + ""
                                                            });

            return View(articleModel);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s
                                  where int.TryParse(s1,
                                                     out guid)
                                  select int.Parse(s1)).ToList();

                if (UnitOfWork.ArticleRepository.CheckDeleteArticle(list))
                {
                    UnitOfWork.ArticleRepository.Delete(list);
                    UnitOfWork.Save();
                }
            }
            return Json(new
                            {
                                status = 1,
                                msg = "Update Successfully"
                            },
                        JsonRequestBehavior.DenyGet);
        }

        #endregion

        #region Section

        public ActionResult Section(int id)
        {
            ViewBag.Id = id;
            return PartialView();
        }

        [GridAction]
        public ActionResult GetSectionList(int id)
        {
            return View(new GridModel(UnitOfWork.SectionRepository.Select(c => c.Article.Id == id && (c.Inactive == false || c.Inactive == null))
                                                .Select(c => new
                                                                 {
                                                                     c.Id,
                                                                     c.Name,
                                                                     c.Priority,
                                                                     c.Type
                                                                 })));
        }

        public ActionResult AddSection(int id)
        {
            return View(new SectionModel
                            {
                                ArticleId = id,
                                Activated = true
                            });
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AddSection(SectionModel sectionModel)
        {
            if (ModelState.IsValid)
            {
                Section section = sectionModel;
                if (UnitOfWork.SectionRepository.CheckInsertSection(section))
                {
                    if (sectionModel.ArticleId.HasValue)
                        section.Article = UnitOfWork.ArticleRepository.SelectbyId(sectionModel.ArticleId.Value);
                    section.Type = InfoTech.VinhSon.Bussiness.Section.SECTION;
                    UnitOfWork.SectionRepository.Insert(section);
                    UnitOfWork.Save();

                    ViewBag.Completed = true;
                }
            }
            return View(sectionModel);
        }

        public ActionResult EditSection(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                Section section = UnitOfWork.SectionRepository.SelectbyId(guid);
                if (section != null)
                {
                    SectionModel sectionModel = section;
                    return View(sectionModel);
                }
            }
            ViewBag.Completed = true;
            return View(new SectionModel());
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditSection(SectionModel sectionModel)
        {
            if (ModelState.IsValid)
            {
                Section section = UnitOfWork.SectionRepository.SelectbyId(sectionModel.Id);
                sectionModel.Update(section);
                if (UnitOfWork.SectionRepository.CheckUpdateSection(section))
                {
                    UnitOfWork.SectionRepository.Update(section);
                    UnitOfWork.Save();

                    ViewBag.Completed = true;
                }
            }
            return View(sectionModel);
        }

        public ActionResult AddDocument(int id)
        {
            return View(new SectionModel
                            {
                                ArticleId = id,
                                Activated = true
                            });
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AddDocument(SectionModel sectionModel)
        {
            if (Request.Files.Count == 0)
                ModelState.AddModelError("Url",
                                         "Upload file is required");
            if (ModelState.IsValid)
            {
                Section section = sectionModel;
                if (UnitOfWork.SectionRepository.CheckInsertSection(section))
                {
                    if (sectionModel.ArticleId.HasValue)
                        section.Article = UnitOfWork.ArticleRepository.SelectbyId(sectionModel.ArticleId.Value);

                    section.Type = InfoTech.VinhSon.Bussiness.Section.DOCUMENT;
                    UnitOfWork.SectionRepository.Insert(section);
                    UnitOfWork.Save();

                    // save document
                    HttpPostedFileBase file = Request.Files[0];
                    if (file != null)
                        if (file.ContentLength > 0)
                        {
                            section.Url = string.Format("{0}_{1}_{2}",
                                                        section.Id,
                                                        ConvertToUnSign(section.Name),
                                                        file.FileName);
                            file.SaveAs(string.Format("{0}{1}",
                                                      Server.MapPath("~/Resource/Document/"),
                                                      section.Url));
                            UnitOfWork.Save();
                        }

                    ViewBag.Completed = true;
                }
            }
            return View(sectionModel);
        }

        public ActionResult EditDocument(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                Section section = UnitOfWork.SectionRepository.SelectbyId(guid);
                if (section != null)
                {
                    SectionModel sectionModel = section;
                    return View(sectionModel);
                }
            }
            ViewBag.Completed = true;
            return View(new SectionModel());
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditDocument(SectionModel sectionModel)
        {
            if (ModelState.IsValid)
            {
                Section section = UnitOfWork.SectionRepository.SelectbyId(sectionModel.Id);

                string fileName = section.Url;

                sectionModel.Update(section);
                if (UnitOfWork.SectionRepository.CheckUpdateSection(section))
                {
                    // save document
                    HttpPostedFileBase file = Request.Files[0];
                    if (file != null)
                        if (file.ContentLength > 0)
                        {
                            // delete old resource
                            if (System.IO.File.Exists(string.Format("{0}{1}",
                                                                   Server.MapPath("~/Resource/Document/"),
                                                                   fileName)))
                                System.IO.File.Delete(string.Format("{0}{1}",
                                                                    Server.MapPath("~/Resource/Document/"),
                                                                    fileName));

                            section.Url = string.Format("{0}_{1}_{2}",
                                                        section.Id,
                                                        ConvertToUnSign(section.Name),
                                                        file.FileName);
                            file.SaveAs(string.Format("{0}{1}",
                                                      Server.MapPath("~/Resource/Document/"),
                                                      section.Url));
                        }
                        else
                            section.Url = fileName;
                    else
                        section.Url = fileName;

                    UnitOfWork.SectionRepository.Update(section);
                    UnitOfWork.Save();

                    ViewBag.Completed = true;
                }
            }
            return View(sectionModel);
        }

        public ActionResult AddImage(int id)
        {
            return View(new SectionModel
                            {
                                ArticleId = id,
                                Activated = true
                            });
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AddImage(SectionModel sectionModel)
        {
            if (Request.Files.Count == 0)
                ModelState.AddModelError("Url",
                                         "Upload file is required");
            if (ModelState.IsValid)
            {
                Section section = sectionModel;
                if (UnitOfWork.SectionRepository.CheckInsertSection(section))
                {
                    if (sectionModel.ArticleId.HasValue)
                        section.Article = UnitOfWork.ArticleRepository.SelectbyId(sectionModel.ArticleId.Value);

                    section.Type = InfoTech.VinhSon.Bussiness.Section.IMAGE;
                    UnitOfWork.SectionRepository.Insert(section);
                    UnitOfWork.Save();

                    // save document
                    HttpPostedFileBase file = Request.Files[0];
                    if (file != null)
                        if (file.ContentLength > 0)
                        {
                            section.Url = string.Format("{0}_{1}_{2}",
                                                        section.Id,
                                                        ConvertToUnSign(section.Name),
                                                        file.FileName);
                            ProcessScaleImage.CenterInside(file.InputStream,
                                                           90,
                                                           60)
                                             .Save(string.Format("{0}{1}",
                                                                 Server.MapPath("~/Resource/Images/thumb_"),
                                                                 section.Url));

                            ProcessScaleImage.CenterInside(file.InputStream,
                                                           section.Width ?? 0,
                                                           section.Height ?? 0)
                                             .Save(string.Format("{0}{1}",
                                                                 Server.MapPath("~/Resource/Images/"),
                                                                 section.Url));
                            UnitOfWork.Save();
                        }

                    ViewBag.Completed = true;
                }
            }
            return View(sectionModel);
        }

        public ActionResult EditImage(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                Section section = UnitOfWork.SectionRepository.SelectbyId(guid);
                if (section != null)
                {
                    SectionModel sectionModel = section;
                    return View(sectionModel);
                }
            }
            ViewBag.Completed = true;
            return View(new SectionModel());
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditImage(SectionModel sectionModel)
        {
            if (ModelState.IsValid)
            {
                Section section = UnitOfWork.SectionRepository.SelectbyId(sectionModel.Id);

                string fileName = section.Url;

                sectionModel.Update(section);
                if (UnitOfWork.SectionRepository.CheckUpdateSection(section))
                {
                    // save document
                    HttpPostedFileBase file = Request.Files[0];
                    if (file != null)
                        if (file.ContentLength > 0)
                        {
                            // delete old resource
                            if (System.IO.File.Exists(string.Format("{0}{1}",
                                                                   Server.MapPath("~/Resource/Images/"),
                                                                   fileName)))
                                System.IO.File.Delete(string.Format("{0}{1}",
                                                                    Server.MapPath("~/Resource/Images/"),
                                                                    fileName));
                            if (System.IO.File.Exists(string.Format("{0}{1}",
                                                                   Server.MapPath("~/Resource/Images/thumb_"),
                                                                   fileName)))
                                System.IO.File.Delete(string.Format("{0}{1}",
                                                                    Server.MapPath("~/Resource/Images/thumb_"),
                                                                    fileName));

                            section.Url = string.Format("{0}_{1}_{2}",
                                                        section.Id,
                                                        ConvertToUnSign(section.Name),
                                                        file.FileName);
                            ProcessScaleImage.CenterInside(file.InputStream,
                                                           90,
                                                           60)
                                             .Save(string.Format("{0}{1}",
                                                                 Server.MapPath("~/Resource/Images/thumb_"),
                                                                 section.Url));
                            ProcessScaleImage.CenterInside(file.InputStream,
                                                           section.Width ?? 0,
                                                           section.Height ?? 0)
                                             .Save(string.Format("{0}{1}",
                                                                 Server.MapPath("~/Resource/Images/"),
                                                                 section.Url));
                        }
                        else
                            section.Url = fileName;
                    else
                        section.Url = fileName;

                    UnitOfWork.SectionRepository.Update(section);
                    UnitOfWork.Save();

                    ViewBag.Completed = true;
                }
            }
            return View(sectionModel);
        }

        public ActionResult AddVideo(int id)
        {
            return View(new SectionModel
                            {
                                ArticleId = id,
                                Activated = true
                            });
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AddVideo(SectionModel sectionModel)
        {
            if (string.IsNullOrEmpty(sectionModel.Url))
                ModelState.AddModelError("Url",
                                         "Youtube link is required");
            if (ModelState.IsValid)
            {
                Section section = sectionModel;
                if (UnitOfWork.SectionRepository.CheckInsertSection(section))
                {
                    if (sectionModel.ArticleId.HasValue)
                        section.Article = UnitOfWork.ArticleRepository.SelectbyId(sectionModel.ArticleId.Value);
                    section.Type = InfoTech.VinhSon.Bussiness.Section.VIDEO;
                    UnitOfWork.SectionRepository.Insert(section);
                    UnitOfWork.Save();

                    ViewBag.Completed = true;
                }
            }
            return View(sectionModel);
        }

        public ActionResult EditVideo(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                Section section = UnitOfWork.SectionRepository.SelectbyId(guid);
                if (section != null)
                {
                    SectionModel sectionModel = section;
                    return View(sectionModel);
                }
            }
            ViewBag.Completed = true;
            return View(new SectionModel());
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditVideo(SectionModel sectionModel)
        {
            if (string.IsNullOrEmpty(sectionModel.Url))
                ModelState.AddModelError("Url",
                                         "Youtube link is required");
            if (ModelState.IsValid)
            {
                Section section = UnitOfWork.SectionRepository.SelectbyId(sectionModel.Id);
                sectionModel.Update(section);
                if (UnitOfWork.SectionRepository.CheckUpdateSection(section))
                {
                    UnitOfWork.SectionRepository.Update(section);
                    UnitOfWork.Save();

                    ViewBag.Completed = true;
                }
            }
            return View(sectionModel);
        }

        [HttpPost]
        public ActionResult DeleteSection(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s
                                  where int.TryParse(s1,
                                                     out guid)
                                  select int.Parse(s1)).ToList();

                if (UnitOfWork.SectionRepository.CheckDeleteSection(list))
                {
                    UnitOfWork.SectionRepository.Delete(list);
                    UnitOfWork.Save();
                }
            }
            return Json(new
                            {
                                status = 1,
                                msg = "Update Successfully"
                            },
                        JsonRequestBehavior.DenyGet);
        }

        public ActionResult Blank()
        {
            return Content("");
        }

        #endregion

        #region Option

        public ActionResult Option(ArticleModel model)
        {
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult UpdateOption(int id,
                                         int key,
                                         bool status)
        {
            Article article = UnitOfWork.ArticleRepository.SelectbyId(id);
            if (article != null)
            {
                if (!article.Status.HasValue)
                    article.Status = 0;
                if (status)
                    article.Status = article.Status.Value | key;
                else
                    article.Status = article.Status.Value & ~key;
                UnitOfWork.Save();
            }
            return Json(new
                            {
                                status = 1,
                                msg = "Update Successfully"
                            },
                        JsonRequestBehavior.DenyGet);
        }

        #endregion

        #region Static Group

        public ActionResult Group(ArticleModel model)
        {
            ViewBag.Id = model.Id;
            ViewBag.Ids = UnitOfWork.ArticleRepository.Select(c => c.Id == model.Id)
                                    .SelectMany(c => c.Groups)
                                    .Where(c => c.Inactive == false || c.Inactive == null)
                                    .Select(c => c.Id)
                                    .ToList();
            return PartialView(UnitOfWork.GroupRepository.Select(c => (c.FilterByDate == false || c.FilterByDate == null) && (c.Inactive == false || c.Inactive == null) && (c.NoArticle == false || c.NoArticle == null)));
        }

        [HttpPost]
        public ActionResult UpdateGroup(int id,
                                        string ids)
        {
            if (!string.IsNullOrEmpty(ids))
            {
                string[] s = ids.Split(',');
                int guid;
                List<int> list = (from s1 in s
                                  where int.TryParse(s1,
                                                     out guid)
                                  select int.Parse(s1)).ToList();

                Article article = UnitOfWork.ArticleRepository.Select(c => c.Id == id,
                                                                      "Groups")
                                            .FirstOrDefault();
                if (article != null)
                {
                    while (article.Groups.Count > 0)
                        article.Groups.Remove(article.Groups.First());
                    var groups = UnitOfWork.GroupRepository.Select(c => list.Contains(c.Id));
                    foreach (Group @group in groups)
                    {
                        article.Groups.Add(group);
                    }
                    UnitOfWork.Save();
                }
            }
            return Json(new
                            {
                                status = 1,
                                msg = "Update Successfully"
                            },
                        JsonRequestBehavior.DenyGet);
        }

        #endregion

        #region Dynamic Group

        public ActionResult DynamicGroup(int id)
        {
            ViewBag.Id = id;
            return PartialView();
        }

        [GridAction]
        public ActionResult GetGroupList(int id)
        {
            return View(new GridModel(UnitOfWork.ArticleGroupRepository.Select(c => c.Inactive == false && c.Article.Id == id)
                                                .Select(c => new
                                                {
                                                    c.Id,
                                                    c.Group.Name,
                                                    c.FromDate,
                                                    c.ToDate
                                                })));
        }

        public ActionResult AddArticleGroup(int id)
        {
            ViewBag.Groups = UnitOfWork.GroupRepository.Select(c => c.Inactive == false && c.FilterByDate == true,
                                                                    c => c.Name)
                                         .Select(c => new
                                         {
                                             c.Id,
                                             c.Name
                                         })
                                         .ToList()
                                         .Select(c => new SelectListItem
                                         {
                                             Text = c.Name,
                                             Value = c.Id + ""
                                         });
            return View(new ArticleGroupModel
                            {
                                ArticleId = id
                            });
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AddArticleGroup(ArticleGroupModel articleGroupModel)
        {
            if (articleGroupModel.FromDate.HasValue && articleGroupModel.ToDate.HasValue)
                if (articleGroupModel.FromDate.Value > articleGroupModel.ToDate.Value)
                    ModelState.AddModelError("ToDate", "ToDate must be greater then FromDate");
            if (ModelState.IsValid)
            {
                ArticleGroup articleGroup = articleGroupModel;
                if (UnitOfWork.ArticleGroupRepository.CheckInsertArticleGroup(articleGroup))
                {
                    articleGroup.Article = UnitOfWork.ArticleRepository.SelectbyId(articleGroupModel.ArticleId ?? 0);
                    articleGroup.Group = UnitOfWork.GroupRepository.SelectbyId(articleGroupModel.GroupId ?? 0);
                    UnitOfWork.ArticleGroupRepository.Insert(articleGroup);
                    UnitOfWork.Save();

                    ViewBag.Completed = true;
                }
            }
            ViewBag.Groups = UnitOfWork.GroupRepository.Select(c => c.Inactive == false && c.FilterByDate == true,
                                                                  c => c.Name)
                                       .Select(c => new
                                       {
                                           c.Id,
                                           c.Name
                                       })
                                       .ToList()
                                       .Select(c => new SelectListItem
                                       {
                                           Text = c.Name,
                                           Value = c.Id + ""
                                       });
            return View(articleGroupModel);
        }

        public ActionResult EditArticleGroup(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                ArticleGroup articleGroup = UnitOfWork.ArticleGroupRepository.SelectbyId(guid);
                if (articleGroup != null)
                {
                    ViewBag.Groups = UnitOfWork.GroupRepository.Select(c => c.Inactive == false && c.FilterByDate == true,
                                                                  c => c.Name)
                                       .Select(c => new
                                       {
                                           c.Id,
                                           c.Name
                                       })
                                       .ToList()
                                       .Select(c => new SelectListItem
                                       {
                                           Text = c.Name,
                                           Value = c.Id + ""
                                       });

                    ArticleGroupModel articleGroupModel = articleGroup;
                    return View(articleGroupModel);
                }
            }
            return Content("");
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditArticleGroup(ArticleGroupModel articleGroupModel)
        {
            if (articleGroupModel.FromDate.HasValue && articleGroupModel.ToDate.HasValue)
                if (articleGroupModel.FromDate.Value > articleGroupModel.ToDate.Value)
                    ModelState.AddModelError("ToDate", "ToDate must be greater then FromDate");
            if (ModelState.IsValid)
            {
                ArticleGroup articleGroup = UnitOfWork.ArticleGroupRepository.SelectbyId(articleGroupModel.Id);
                articleGroupModel.Update(articleGroup);
                if (UnitOfWork.ArticleGroupRepository.CheckUpdateArticleGroup(articleGroup))
                {
                    articleGroup.Article = UnitOfWork.ArticleRepository.SelectbyId(articleGroupModel.ArticleId ?? 0);
                    articleGroup.Group = UnitOfWork.GroupRepository.SelectbyId(articleGroupModel.GroupId ?? 0);
                    UnitOfWork.ArticleGroupRepository.Update(articleGroup);
                    UnitOfWork.Save();

                    ViewBag.Completed = true;
                }
            }
            ViewBag.Groups = UnitOfWork.GroupRepository.Select(c => c.Inactive == false && c.FilterByDate == true,
                                                                  c => c.Name)
                                       .Select(c => new
                                       {
                                           c.Id,
                                           c.Name
                                       })
                                       .ToList()
                                       .Select(c => new SelectListItem
                                       {
                                           Text = c.Name,
                                           Value = c.Id + ""
                                       });
            return View(articleGroupModel);
        }

        [HttpPost]
        public ActionResult DeleteArticleGroup(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s
                                  where int.TryParse(s1,
                                                      out guid)
                                  select int.Parse(s1)).ToList();

                if (UnitOfWork.ArticleGroupRepository.CheckDeleteArticleGroup(list))
                {
                    UnitOfWork.ArticleGroupRepository.Delete(list);
                    UnitOfWork.Save();
                }
            }
            return Json(new
                            {
                                status = 1,
                                msg = "Update Successfully"
                            },
                        JsonRequestBehavior.DenyGet);
        }

        #endregion

    }
}
